<template>
  <div id="container">
    <el-upload
      class="avatar-uploader"
      :action="serverUrl"
      name="file"
      :show-file-list="false"
      list-type="picture"
      :multiple="false"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :before-upload="beforeUpload"
    >
    </el-upload>

    <el-form :model="addForm" label-position="left" ref="addForm">
      <el-form-item label="公告标题">
        <el-input
          v-model="addForm.noticeTitle"
          placeholder="请输入公告标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="公告内容">
        <quill-editor
          class="editor"
          ref="myQuillEditor"
          v-model="addForm.noticeContent"
          :options="editorOption"
        >
        </quill-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  ["blockquote", "code-block"], // 引用  代码块
  [{ header: 1 }, { header: 2 }], // 1、2 级标题
  [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  [{ script: "sub" }, { script: "super" }], // 上标/下标
  [{ indent: "-1" }, { indent: "+1" }], // 缩进
  // [{'direction': 'rtl'}],                         // 文本方向
  [{ size: ["small", false, "large", "huge"] }], // 字体大小
  [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  [{ font: [] }], // 字体种类
  [{ align: [] }], // 对齐方式
  ["clean"], // 清除文本格式
  ["link", "image", "video"], // 链接、图片、视频
];
export default {
  data() {
    return {
      addForm: {},
      serverUrl: "http://localhost:9999/upload?flag=1",
      quillUpdateImg: false,
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions,
            // container: "#toolbar",
            handlers: {
              image: function (value) {
                alert(value);
                if (value) {
                  // 触发input框选择图片文件
                  document.querySelector(".avatar-uploader input").click();
                } else {
                  this.quill.format("image", false);
                }
              },
            },
          },
        },
      },
    };
  },
  created() {
    let id = this.$route.query.id;
    if (id != null && id != undefined) {
      this.getNoticeById(id);
    }
  },
  methods: {
    async submitForm() {
      this.addForm.noticeTime = new Date();
      this.addForm.publisher = localStorage.getItem("managerName");
      let id = this.$route.query.id;
      if (id) {
        const { data: res } = await this.$http.post(
          "common/notice/notice/editNotice",
          this.addForm
        );
        if (res.code != 200) {
          return this.$message.error(res.message);
        } else {
          this.$router.push("/notice");
        }
      } else {
        const { data: res } = await this.$http.post(
          "common/notice/notice/saveNotice",
          this.addForm
        );
        if (res.code != 200) {
          return this.$message.error(res.message);
        } else {
          this.$router.push("/notice");
        }
      }
    },
    // 富文本图片上传前
    beforeUpload() {
      // 显示loading动画
      this.quillUpdateImg = true;
    },

    uploadSuccess(res, file) {
      // res为图片服务器返回的数据
      // 获取富文本组件实例
      let quill = this.$refs.myQuillEditor.quill;
      // 如果上传成功
      if (res.code == 200) {
        // 获取光标所在位置
        let length = quill.getSelection().index;
        // 插入图片  res.url为服务器返回的图片地址
        quill.insertEmbed(length, "image", res.data.url);
        // 调整光标到最后
        quill.setSelection(length + 1);
      } else {
        this.$message.error("图片插入失败");
      }
      // loading动画消失
      this.quillUpdateImg = false;
    },
    // 富文本图片上传失败
    uploadError() {
      // loading动画消失
      this.quillUpdateImg = false;
      this.$message.error("图片插入失败");
    },
    async getNoticeById(id) {
      const { data: res } = await this.$http.get(
        "common/notice/notice/getNoticeById",
        { params: { noticeId: id } }
      );
      if (res.code != 200) {
        return this.message.error(res.message);
      } else {
        this.addForm = res.data;
      }
    },
  },
};
</script>
<style lang="less" scoped>
/deep/.ql-editor {
  height: 300px;
}
/deep/.el-form-item__content {
  margin-top: 50px;
}
</style>